<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/19
 * Time: 14:58
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class MaterialModel
{
    const TABLE = 'material';

    public function lists(array $param =[],$exports = false)
    {
         $result = DB::table(self::TABLE)
             ->leftJoin('material_category as mc','mc.id','=',self::TABLE.'.categoryId')
             ->where(function ($query) use ($param) {
                 if (isset($param['search']) && !is_null($param['search'])) {
                     $query->where(function ($query1) use ($param){
                         $query1->where(self::TABLE.'.name', 'like', '%' . $param['search'] . '%');
                     });
                 }
                 if (!empty($param['categoryId'])) {
                     $query->where(self::TABLE.'.categoryId', $param['categoryId']);
                 }
             });

         if (!$exports){
             $limit = config('yucheng.limit');
             $start = 0;
             if (!empty($param['length'])) {
                 $limit = $param['length'];
             }
             if (!empty($param['page'])) {
                 $start = ($param['page']-1)*$limit;
             }
             $result = $result->offset($start)->limit($limit);
         }

         return $result->select(self::TABLE.'.*','mc.name as categoryName')
             ->orderBy('createTime','desc')
             ->orderBy('order','desc')
             ->get()->toArray();
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function countLists(array $param)
    {
        return DB::table(self::TABLE)
            ->where(function ($query) use ($param) {
                if (isset($param['search']) && !is_null($param['search'])) {
                    $query->where(function ($query1) use ($param){
                        $query1->where('name', 'like', '%' . $param['search'] . '%');
                    });
                }
                if (!empty($param['categoryId'])) {
                    $query->where(self::TABLE.'.categoryId', $param['categoryId']);
                }
            })
            ->count();
    }

    /**
     * @param array $check
     * @param $id
     * @return array
     */
    public function checkRepeat(array $check, $id = 0)
    {
        $result = DB::table(self::TABLE)->where($check)
            ->where(function ($query) use ($id) {
                if ($id != 0) {
                    $query->where('id', '!=', $id);
                }
            })
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        $data['createTime'] = date('Y-m-d H:i:s');
        return DB::table(self::TABLE)->insertGetId($data);
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    public function addMaterial(array $param)
    {
        DB::beginTransaction();
        try{
            $param['code'] = empty($param['code'])?'00':$param['code'];
            $param['createTime'] = date('Y-m-d H:i:s');
            DB::table('material_category')->where('id',$param['categoryId'])->increment('increment');
            $insertId = DB::table(self::TABLE)->insert($param);
            DB::commit();
            return $insertId;
        }catch(\Exception $e){
            Log::info($e->getMessage() . ' in ' . $e->getFile() . '(' . $e->getLine() . ')');
            DB::rollBack();
        }
    }

    public function delete(array $param)
    {
        return DB::table(self::TABLE)->where($param)->delete();
    }

    public function editStatus($materialId,$status)
    {
        DB::beginTransaction();
        try{
            $this->update(['id'=>$materialId],['status'=>$status]);
            DB::table('material_spec')->where('materialId',$materialId)->update(['status'=>$status]);
            DB::commit();
        }catch(\Exception $e){
            DB::rollBack();
        }
    }

    public function selectLists(array $param)
    {
        return DB::table(self::TABLE)
            ->leftJoin('material_category as mc','mc.id','=',self::TABLE.'.categoryId')
            ->where(function ($query) use ($param) {
                if (!empty($param['categoryId'])) {
                    $query->where(self::TABLE.'.categoryId', $param['categoryId']);
                }
            })
            ->select(self::TABLE.'.*','mc.name as categoryName')
            ->orderBy('order','desc')
            ->get()->toArray();
    }
}